package mysql

import (
	"encoding/json"
	"errors"
	"testGin/model/tool"
)

type Files struct {
	Id         uint `gorm:"column:id;primary_key" json:"id" form:"id"`
	Status     int  `gorm:"column:status" json:"status" form:"status"`
	Deleted    int  `gorm:"column:deleted" json:"deleted"`
	AddTime    int32 `gorm:"column:add_time" json:"add_time"`
	UpdateTime int32 `gorm:"column:update_time" json:"update_time"`
	Memo       string `gorm:"column:memo" json:"memo" form:"memo"`
	Name     string `gorm:"column:name" json:"name"`
	Md5  string `gorm:"column:md5" json:"md5"`
	IdAdmin uint `gorm:"column:id_admin" json:"id_admin"`
	Savename string `gorm:"column:savename" json:"savename"`
	Savepath string `gorm:"column:savepath" json:"savepath"`
	ImagePath string `gorm:"column:image_path" json:"image_path"`
	ImageUrl string `gorm:"column:image_url" json:"image_url"`
	Ext string `gorm:"column:ext" json:"ext"`
	Mime string `gorm:"column:mime" json:"mime"`
	Size int64 `gorm:"column:size" json:"size"`
	Sha1    string `gorm:"column:sha1" json:"sha1"`
}
//这一步的意思是改表明的  默认表明是带s的
func (Files) TableName() string  {
	return "k_admin_sys_file"
}

func (model Files) GetList(where []map[string]interface{},page int,pageSize int) (map[string]interface{},error){

	if page <= 0 {
		return nil,errors.New("page 不正确")
	}
	if page == 1 {
		page = 0
	}
	result := map[string]interface{}{}
	db :=Db
	num := 0
	adminsArray := []Files{}
	offset := (page-1)*pageSize


	for _, m := range where {
		db = db.Where(m["filed"].(string)+" "+m["exp"].(string)+" ?",m["val"])
	}

	err := db.Model(&Files{}).Count(&num).Limit(pageSize).Offset(offset).Find(&adminsArray).Error
	if err == nil{
		result["data"] = adminsArray
		result["count"] = num
	}
	return result,err;

}

func (model Files) GetListPage(where []map[string]interface{},page int,pageSize int) (map[string]interface{},error){

	if page <= 0 {
		return nil,errors.New("page 不正确")
	}
	if page == 1 {
		page = 1
	}
	result := map[string]interface{}{}
	db :=Db
	num := 0
	adminsArray := []Files{}
	offset := (page-1)*pageSize


	for _, m := range where {
		db = db.Where(m["filed"].(string)+" "+m["exp"].(string)+" ?",m["val"])
	}



	err := db.Model(&Files{}).Count(&num).Limit(pageSize).Offset(offset).Find(&adminsArray).Error

	url := "/system/upload/flist"

	data := adminsArray

	ldata := []map[string]interface{}{}

	for _, files := range data {
		res,_ :=jsonStructToMap(&files)
		ldata = append(ldata,res)
	}


	count:= num

	defaultConfig := map[string]interface{}{
		"query"     : []string{}, //url额外参数
		"fragment"  : "", //url锚点
		"var_page"  : "page", //分页变量
		"list_rows" : 15, //每页数量
	}

	config := defaultConfig
	config["path"] = url


	presult := tool.Paginator{}.Create(ldata,pageSize,page,count,config)

	var boot tool.Bootstrap = tool.Bootstrap{
		presult,
	}

	if err == nil{
		result["data"] = adminsArray
		result["page"] = boot.Render()
	}
	return result,err;

}


func (n Files) Add(Admins Files)error{
	db := Db
	err := db.Create(&Admins).Error
	return err
}

func (model Files) Save(Admins Files)error{
	db := Db
	err := db.Save(&Admins).Error
	return err
}


func jsonStructToMap(stuObj *Files) (map[string]interface{}, error){
	// 结构体转json
	strRet, err := json.Marshal(stuObj)
	if err != nil{
		return nil, err
	}
	// json转map
	var mRet map[string]interface{}
	err1 := json.Unmarshal(strRet, &mRet)
	if err1 != nil{
		return nil, err1
	}
	return mRet, nil
}





